package com.example.smartagrisys.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.smartagrisys.entity.Complaint;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 投诉Mapper接口
 */
@Mapper
public interface ComplaintMapper extends BaseMapper<Complaint> {
    
    /**
     * 获取买家的投诉列表
     *
     * @param buyerId 买家ID
     * @return 投诉列表
     */
    @Select("SELECT c.*, u.username AS seller_name FROM complaint c " +
            "LEFT JOIN sys_user u ON c.seller_id = u.id " +
            "WHERE c.buyer_id = #{buyerId} AND c.deleted = false " +
            "ORDER BY c.create_time DESC")
    List<Complaint> selectByBuyerId(@Param("buyerId") Long buyerId);
    
    /**
     * 获取卖家的投诉列表
     *
     * @param sellerId 卖家ID
     * @return 投诉列表
     */
    @Select("SELECT c.*, u.username AS buyer_name FROM complaint c " +
            "LEFT JOIN sys_user u ON c.buyer_id = u.id " +
            "WHERE c.seller_id = #{sellerId} AND c.deleted = false " +
            "ORDER BY c.create_time DESC")
    List<Complaint> selectBySellerId(@Param("sellerId") Long sellerId);
    
    /**
     * 根据状态查询投诉列表
     *
     * @param status 投诉状态
     * @return 投诉列表
     */
    @Select("SELECT c.*, u1.username AS buyer_name, u2.username AS seller_name " +
            "FROM complaint c " +
            "LEFT JOIN sys_user u1 ON c.buyer_id = u1.id " +
            "LEFT JOIN sys_user u2 ON c.seller_id = u2.id " +
            "WHERE c.status = #{status} AND c.deleted = false " +
            "ORDER BY c.create_time DESC")
    List<Complaint> selectByStatus(@Param("status") String status);

    /**
     * 根据ID获取投诉详情
     *
     * @param id 投诉ID
     * @return 投诉详情
     */
    @Select("SELECT c.*, u1.username AS buyer_name, u2.username AS seller_name " +
            "FROM complaint c " +
            "LEFT JOIN sys_user u1 ON c.buyer_id = u1.id " +
            "LEFT JOIN sys_user u2 ON c.seller_id = u2.id " +
            "WHERE c.id = #{id} AND c.deleted = false")
    Complaint selectComplaintDetailById(@Param("id") Long id);
} 